<template>
  <div class="login-container">
    <div class="formBox">
      <div class="enTitle">welcome</div>
      <div class="titleBox">
        <div class="bigTitle">欢迎登录大山AI系统</div>
        <div class="smallTitle">快捷有效、安全稳定、赋能智慧未来</div>
      </div>
      <el-form ref="loginFormRef" :disabled="loading" :model="formData" :rules="loginFormRules">
        <el-form-item prop="account">
          <el-input v-model.trim="formData.account" size="large" placeholder="帐号/邮箱/手机号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            size="large"
            v-model="formData.password"
            placeholder="密码"
            @keyup.enter="loginEvent"
          ></el-input>
        </el-form-item>
        <el-button :loading="loading" type="primary" size="large" @click.prevent="loginEvent"> 登录 </el-button>
      </el-form>
    </div>
    <div class="img"></div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'

import ThemeSwitch from '@/components/ThemeSwitch/index.vue'
import { login, type LoginResult } from '@/api/user'
import { setToken } from '@/utils/cache'

const formData = ref({
  account: '',
  password: ''
})
const loginFormRules = {
  account: [{ required: true, message: '请输入帐号/邮箱/手机号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}
const loginFormRef = ref()
const loading = ref<boolean>(false)

const router = useRouter()
const route = useRoute()

const loginEvent = () => {
  if (loginFormRef.value) {
    loginFormRef.value.validate(async (valid: boolean) => {
      if (valid) {
        loading.value = true
        const res = await login(formData.value)
        loading.value = false
        if (res?.code === 200) {
          const data = res.data as LoginResult
          setToken(data.accessToken, data.refreshToken)
          router.replace((route.query?.redirect || '/') as string)
        } else {
          ElMessage({ message: res?.msg || '网络异常，请稍后重试', type: 'error' })
        }
      }
    })
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: 100%;
  background-image: url("@/assets/img/loginBg.jpg");
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;

  .formBox {
    width: 710px;
    padding: 0 0 0 120px;
    cursor: pointer;

    .enTitle {
      font-size: 30px;
      color: #ddd;
      font-weight: bold;
      font-family: Arial, Helvetica, sans-serif;
    }

    .titleBox {
      margin: 50px 0;
      
      .bigTitle {
        font-size: 48px;
        color: #333;
        font-weight: bold;
      }
      .smallTitle {
        font-size: 20px;
        color: #999;
        margin-top: 8px
      }
    }

    .el-form {
      width: 390px
    }

    .el-button {
      width: 100%;
    }
  }

  .img {
    width: calc(100% - 710px);
    height: 100vh;
    position: relative;
    background-image: url("@/assets/img/loginImg.jpg");
    background-size: cover;
    background-position: center center;
    background-repeat: no-repeat;
  }
}

@media (max-width: 1440px) {
  .login-container .formBox {
    width: 580px;
    padding: 0 0 0 80px
  }
  .login-container .formBox .titleBox {
    margin: 35px 0
  }
  .login-container .formBox .titleBox .bigTitle {
    font-size: 40px
  }
  .login-container .img {
    width: calc(100% - 580px)
  }
}

@media (max-width: 1200px) {
  .login-container .formBox {
    width: 100%;
    padding: 0 20px
  }
  .login-container .formBox .enTitle {
    font-size: 22px
  }
  .login-container .formBox .titleBox {
    margin: 10px 0 15px
  }
  .login-container .formBox .titleBox .bigTitle {
    font-size: 32px
  }
  .login-container .formBox .titleBox .smallTitle {
    font-size: 16px
  }
  .login-container .img {
    display: none;
  }
  .login-container .formBox .el-form {
    width: 100%
  }
}

@media (max-width: 780px) {
  .login-container .formBox .enTitle {
    font-size: 18px
  }
  .login-container .formBox .titleBox {
    margin: 6px 0 25px
  }
  .login-container .formBox .titleBox .bigTitle {
    font-size: 24px
  }
  .login-container .formBox .titleBox .smallTitle {
    font-size: 14px;
    margin-top: 3px;
  }
  .login-container .img {
    display: none;
  }
  .login-container .formBox .el-form {
    width: 100%
  }
}
</style>
